package com.ruoyi.order.bloonfilter;


import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.mapper.TbOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;


@Configuration
public class BlooInit implements CommandLineRunner {
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Override
    public void run(String... args) throws Exception {

    }

    //配置bean
    @Bean
    public BloomFilter bloomFilter(){
        // 初始化布隆过滤器，设置数据类型，数组长度和误差值
        BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 1000000L, 0.01);
        //获取要装入过滤器的数据
        List<TbOrder> tbOrderList = tbOrderMapper.selectTbOrderList(null);

        tbOrderList.forEach(tbOrder -> {
            //吧订单id数据存入到布隆过滤器
            bloomFilter.put(tbOrder.getId().toString());
        });
        return bloomFilter;

    }
}
